package com.le.tester.leetcode;


import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

/**
 * 描述：字符串的排列
 */
public class Permutation {

    public Set<String> set;

    public int length;

    public boolean[] useArray;

    public String str;

    public String[] permutation(String s) {
        set = new HashSet<>();
        this.length = s.length();
        useArray = new boolean[length];
        this.str = s;

        create(new StringBuilder(""));
        return set.toArray(new String[0]);
    }

    public void create(StringBuilder stringBuilder) {
        if (stringBuilder.length() == length) {
            set.add(stringBuilder.toString());
            return;
        }
        for (int i = 0; i < length; i++) {
            if (useArray[i]) {
                continue;
            }
            stringBuilder.append(stringBuilder.charAt(i));
            useArray[i] = true;
            create(stringBuilder);
            useArray[i] = false;
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);

        }

    }

    public static void main(String[] args) {
        Permutation permutation = new Permutation();
        System.out.println(Arrays.toString(permutation.permutation("abc")));
    }
}
